package com.ryan.mms.privilage.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ryan.framework.dao.impl.BaseHibernateDaoSupport;
import com.ryan.framework.tag.pagination.Page;
import com.ryan.mms.privilage.dao.OperatorDao;
import com.ryan.mms.privilage.model.Operator;
import com.ryan.utils.StringUtil;

@Repository
public class OperatorDaoImpl extends BaseHibernateDaoSupport<Operator, Long> implements OperatorDao<Operator,Long> {
	private final String deleteOperatorSql = "delete from Operator operator where operator.operatorId in (?)";

	public Operator findByLoginNameAndPassword(Operator operator) {
		Criterion [] criterions = new Criterion[2];
		criterions[0]=Restrictions.eq("loginName", operator.getLoginName());
		criterions[1]=Restrictions.eq("password", operator.getPassword());
		List<Operator> operatorList = this.find(criterions);
		Operator operatorResult =null;
		if(null!=operatorList&&operatorList.size()==1){
			operatorResult = operatorList.get(0);
		}
		return operatorResult;
	}

	public List<Operator> findByOperator(Operator operator) {
		List<Operator> userList = this.find(generateCriterionList(operator));
		return userList;
	}

	public List<Operator> findByOperator(Page page,Operator operator) {
		List<Operator> userList = this.find(page,generateCriterionList(operator));
		return userList;
	}
	
	private List<Criterion> generateCriterionList(Operator operator){
		List<Criterion> criterionList=new ArrayList<Criterion>();
		if(!StringUtil.isNull(operator.getLoginName())){
			criterionList.add(Restrictions.eq("loginName", operator.getLoginName()));
		}
		if(!StringUtil.isNull(operator.getPassword())){
			criterionList.add(Restrictions.eq("password", operator.getPassword()));
		}
		if(!StringUtil.isNull(operator.getStatus())){
			criterionList.add(Restrictions.eq("status", operator.getStatus()));
		}
		if(!StringUtil.isNull(operator.getMember())){
			criterionList.add(Restrictions.eq("member", operator.getMember()));
		}
		return criterionList;
	}

	public void deleteByOperatorIds(List<Long> pks) {
		for(String pksStr :getPksList(pks)){
			super.execute(deleteOperatorSql, pksStr);
		}
	}


	public void deleteByOperatorIds(String pks) {
		super.execute(deleteOperatorSql, pks);
	}

	public List<String> getPksList(List<Long> pks){
		List<String> idsList = new ArrayList<String>();
		StringBuilder pksStrBuilder = new StringBuilder();
		int i=0;
		for(Long pk:pks){
			pksStrBuilder.append(",").append(pk);
			if(i==998){
				String pksStr=pksStrBuilder.toString();
				pksStr = pksStr.substring(1);
				idsList.add(pksStr);
				pksStrBuilder = new StringBuilder();
				i=-1;
			}
		}
		if(i==998){
			String pksStr = pksStrBuilder.toString();
			if(null!=pksStr&&!"".equals(pksStr)){
				pksStr = pksStr.substring(1);
			}
		}
		return idsList;
	}
}
